﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AppFramework.DinosaursParkMes.Dictionaries.Dto;
using System.Runtime.InteropServices;
using AutoMapper;
using NPOI.SS.Formula.Functions;
using AppFramework.DinosaursParkMes.DictionariesModel;
using Snowflake.Core;
using AppFramework.DinosaursParkMes.DictionariesModel.IService;
using Microsoft.EntityFrameworkCore;

namespace AppFramework.DinosaursParkMes.Dictionaries
{
    public class DictionariesAppService : AppFrameworkAppServiceBase, IDictionariesAppService
    {
        private readonly IRepository<DictionariesModel.Dictionaries> repository;
        private readonly IRepository<DictionariesModel.Dictionarieslist>  repository1;
        private readonly IDictionariesService dictionariesService;
        public DictionariesAppService(IRepository<DictionariesModel.Dictionaries> repository, IRepository<DictionariesModel.Dictionarieslist> repository1 = null, IDictionariesService dictionariesService = null)
        {
            this.repository = repository;
            this.repository1 = repository1;
            this.dictionariesService = dictionariesService;
        }
        /// <summary>
        /// 查询字典值
        /// </summary>
        /// <param name="DicId"></param>
        /// <returns></returns>
        [HttpGet,Route("GetDictionary")]
        public async  Task<List<DictionarieslistDto>> GetDictionary(string  DicId)
        {
            List<Dictionarieslist> list = await dictionariesService.GetDictionary(DicId);
            return ObjectMapper.Map<List<DictionarieslistDto>>(list);
        }
        /// <summary>
        /// 字典新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost,Route("InsertDictionary")]
        public async  Task<AddDictionariesDto> InsertDictionary(AddDictionariesDto dto)
        {
            //转换字典目录
            DictionariesModel. Dictionaries catalog = ObjectMapper.Map<DictionariesModel.Dictionaries>(dto);
            catalog.Remark = new IdWorker(1, 1).NextId().ToString();
            foreach (var item in dto.children)
            {
                item.Remark = catalog.Remark;
            }
            //转换字典详情
            catalog.DictionaryDetails = ObjectMapper.Map<List<Dictionarieslist>>(dto.children);
            //调用添加字典方法
            var main = await dictionariesService.InsertDictionary(catalog);

            AddDictionariesDto result = ObjectMapper.Map<AddDictionariesDto>(main);
            result.children = ObjectMapper.Map<List<DictionarieslistItemDto>>(main.DictionaryDetails);
            return result;
        }
        /// <summary>
        /// 字典列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("Show")]
        public async Task<dynamic>  Show()
        {
            var list = repository.GetAll().Select(x=>new {
                x.Id,
                x.Dictionariesname,
                x.CreationTime,
            });
             return await list.ToListAsync();
        }
    }
}
